<?php
/**
 * Created by PhpStorm.
 * Author: QinShuai
 * Email: qinshuai0423@qq.com
 * Date: 2021/6/15
 */

namespace backend\controllers;

use backend\models\AdminRole;
use backend\models\AdminRoleUser;
use backend\traits\Controller;

class AdminUserController extends BaseController
{
    use Controller;

    /**
     * 过滤器
     * @param $map
     */
    public function filter(&$map)
    {
        $map['_order'] = 'u.id';
        $map['_field'] = ['u.*', 'r.user_id', 'r.role_id', 'role.name'];
        $map['u.isdelete'] = 0;
        $map['_func'] = function ($model) {
            $model->alias('u')->leftJoin(AdminRoleUser::tableName() . ' r', 'r.user_id = u.id')
                ->leftJoin(AdminRole::tableName() . ' role', 'role.id = r.role_id');
        };

    }

    public function beforeAction($action)
    {
        $roles = (new AdminRole())->getRoles();
        $this->data['roles'] = json_encode($roles);
        return parent::beforeAction($action);
    }

}